<?php

class ContactController {

    public function invoke() {

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }


        if ($action == 'sendContact') {
            $security = $_POST['txtSecurityCode'];
            if ($security != $_SESSION["sSercurityCode"]) {
                Utility::messageBox('Mã xác nhận không hợp lệ', './com_contact/contact.htm');
            } else {

                $fullName = $_POST['txtFullName'];
                $email = $_POST['txtEmail'];
                $company = $_POST['txtCompany'];
                $address = $_POST['txtAddress'];
                $phoneNumber = $_POST['txtTelephone'];
                $fax = $_POST['txtFax'];
                $title = $_POST['txtTitle'];
                $content = $_POST['txtContent'];
                $date = Utility::formatDateTime(date("d/m/Y"));

                $result = $this->addContact($fullName, $email, $company, $address, $phoneNumber, $fax, $title, $content, $date);
                if ($result == OK) {
                    Utility::messageBox('Cảm ơn liên hệ của bạn! Chúng tôi sẽ liên hệ với bạn sớm nhất');
                    Utility::redirect("./com_contact/contact.htm");
                }
            }
        } else {
            $derectory = dirname(__FILE__);
            $xtemplate = new XTemplate($derectory . DS . 'views' . DS . 'contact.php');
            $configuration = $this->getConfiguration();

            $xtemplate->assign('SITENAME', $configuration->getSiteName());
            $xtemplate->assign('ADDRESS', $configuration->getAddress());
            $xtemplate->assign('PHONE_NUMBER', $configuration->getPhoneNumber());
            $xtemplate->assign('EMAIL', $configuration->getEmail());
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    private function getConfiguration() {
        try {
            $xmlFile = ROOT . DS . 'configuration.xml';
            $configuration = new Configuration();
            $configuration->getConfiguration($xmlFile);
            return $configuration;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    private function addContact($fullname, $email, $company, $address, $phoneNumber, $fax, $title, $content, $date) {
        $db = new Database();
        try {
            $contact = new Contacts();
            $contact->setFullname($fullname);
            $contact->setEmail($email);
            $contact->setCompany($company);
            $contact->setAddress($address);
            $contact->setPhone($phoneNumber);
            $contact->setFax($fax);
            $contact->setTitle($title);
            $contact->setContent($content);
            $contact->setCreatedDate($date);
            $result = $contact->add($db);
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
        }
    }

}
